<template>
	<!-- 岗位编辑 -->
	<div>
		<div class="edit-top">
			编辑信息
			<i class="el-icon-close" @click='cancel'></i>
		</div>
		<div class="edit-content">
			<div class="edit-title">岗位信息</div>
			<div class="edit-item">
				<label>岗位名称</label><input v-model="info.positionName" type="text" />
			</div>
			<div class="edit-item">
				<label>岗位编码</label><input v-model="info.positionCode" type="text" />
			</div>
			<div class="edit-item">
				<label>所属部门</label><select v-model="info.deptId">
				<option v-for="item in dep_list" :value="item.id" >{{item.deptName}}</option>
				</select>
			</div>
			<div class="edit-item">
				<label>岗位权限</label>
				<div>
					<el-checkbox v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
					<el-tree ref="pos" :data="data" @check="changeTree"  show-checkbox node-key="id" :default-expanded-keys="opendata" :props="defaultProps"
					 >
					</el-tree>
				</div>
			</div>



		</div>
		<div class="edit-bottom">
			<span @click='cancel'>取消</span>
			<el-button @click='save' type="primary">保存</el-button>
		</div>
	</div>
</template>

<script>
	import Global from '@/components/Global.vue'
	export default {
		props:['id'],
		data() {
			return {
				dep_list:[],
				checkAll: false,
				data: [],
				opendata: [1],
				defaultProps: {
					children: 'children',
					label: 'label'
				},
				dep_list: [],
				info: {
					positionName: '',
					positionCode: '',
					// parentId: 0,
					permissionIds:[],
					deptId:'',

				},
				parent_arr:[],
			}
		},
		mounted() {
			this.get_dep_list();
			this.get_permission();
		},
		methods: {
			changeTree(){
				var self = this;
				 setTimeout(function(){self.checkStrictly = false;},1000);
				
				
				//setTimeout("this.checkStrictly = false", 3000 );
				 // this.$nextTick(() => {
				 //        this.checkStrictly = false; //重点： 赋值完成后 设置为false
				 //      });
					  },
				
			get_dep_list(){
				var self = this;
				this.$axios.post(this.$global.serverIp + 'admin/sysDept/findPage', {
						"limit": "10000",
						"page": "1"
					})
					.then(function(res) {
						if (res != '') {
							self.dep_list = res.data.list;
						}
				
					})
			},
			get_info(){
				var self = this;
				this.$axios.get(this.$global.serverIp + 'admin/sysPosition/detail/'+this.id)
					.then(function(res) {
						if (res != '') {
							self.info = res.data;
							var arr = [];
							for(var i=0;i< self.info.permissionIds.length;i++){
								var flag = 0;
								for(var j=0;j<self.parent_arr.length;j++){
									if(self.info.permissionIds[i] == self.parent_arr[j]){
										flag = 1;
										break;
									}
								}
								if(flag == 0){
									arr.push(self.info.permissionIds[i]);
								}
							}
							
							self.$refs.pos.setCheckedKeys(arr);
						}
				
					})
			
			},
			handleCheckAllChange(val) {
				if (this.checkAll) {
					this.$refs.pos.setCheckedNodes(this.data);
				} else {
					this.$refs.pos.setCheckedKeys([]);
				}
			},
			get_permission() {
				var self = this;
				this.$axios.get(this.$global.serverIp + 'admin/permission/selectAllTree')
				  .then(function (res) {
					  if(res != '')
						self.data = self.change_data(res.data);
						self.get_info();
				  })
				
			},
			change_data(list) {
				var data = [];

				var length = list.length;
				for (var i = 0; i < length; i++) {
					var e = {};
					var s_length = list[i].children.length;
					if (s_length > 0) {
						e.children = this.change_data(list[i].children);
						this.parent_arr.push(list[i].id);
					}
					e.id = list[i].id;
					e.label = list[i].permissionName;
					data.push(e);
					this.opendata.push(list[i].id);
				}
				return data;
			},
			save() {
				let res = this.$refs.pos.getCheckedNodes()
				this.info.permissionIds = [];
				res.forEach((item) => {
					this.info.permissionIds.push(item.id)
				});
				res =  this.$refs.pos.getHalfCheckedNodes();
				res.forEach((item) => {
					this.info.permissionIds.push(item.id);
				});
				console.log(this.info);
				// return;
				if (this.info.positionName == "") {
					this.$popup.open('岗位名称不能为空');
					return;
				}
				if (this.info.positionCode == "") {
					this.$popup.open('岗位编码不能为空');
					return;
				}
				var self = this;
				this.$axios.post(this.$global.serverIp + 'admin/sysPosition/save', self.info)
					.then(function(res) {
						if (res != '') {
							self.success('编辑成功');
						}
					})
			},
			success(content) {
				var self = this;
				this.$alert(content, '提示', {
					confirmButtonText: '确定',
					callback: action => {
						self.$emit("saveEdit", 2);
					}
				});
			},
	
			cancel() {
				this.$emit("saveEdit", 1);
			}
		}
	}
	import '@/assets/css/edit.css'
</script>
